//
//  SoundTool.swift
//  whack-a-mole
//
//  Created by wxc on 2019/9/19.
//  Copyright © 2019 wxc. All rights reserved.
//

import UIKit
import AVFoundation
class SoundTool: NSObject {

    var circulating : Bool = false

    var player : AVAudioPlayer?
    
    func playSoundWithURL(soundURL : URL) -> Void {

        do{
            self.player = try AVAudioPlayer.init(contentsOf: soundURL)
        }catch {
            print(error.localizedDescription)
        }
        
        if self.circulating
        {
            self.player?.numberOfLoops = -1
            self.player?.volume = 0.5
        }
        else
        {
            self.player?.volume = 1
        }
        
        self.player?.play()
//        self.player?.prepareToPlay()
//        self.player?.play(atTime: 0)
//        self.player?.currentTime = 0
//        let audioSession = AVAudioSession.sharedInstance()
//        try! audioSession.setCategory(AVAudioSession.Category.playback);
//        try! audioSession.setActive(true)
    
    }
    
    func stopSound() -> Void {
        self.player?.stop()
    }
    
    func pauseSound() -> Void {
        self.player?.pause()
    }
    
    func playSound() -> Void {
        self.player?.play()
    }
    
    func isPlaying() -> Bool {
        return self.player?.isPlaying ?? true
    }
    
}
